import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:mi_mang_flutter/provider/theme_provider.dart';
import 'package:mi_mang_flutter/res/dimens.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';
import 'package:provider/provider.dart';

/// 字体大小设置页面
class SettingFontSizePage extends StatefulWidget {
  SettingFontSizePage({Key? key}) : super(key: key);

  @override
  _SettingFontSizePageState createState() => _SettingFontSizePageState();
}

class _SettingFontSizePageState extends State<SettingFontSizePage> {
  // 默认标准大小
  double sliderValue = 2.0;
  late ThemeProvider provider;
  late double _textScaleFactor;

  @override
  void initState() {
    provider = Provider.of<ThemeProvider>(context, listen: false);
    _textScaleFactor = MediaQuery.of(context).textScaler.scale(1.0);
    // 计算当前字体大小系数的位置:
    // (大小系数-1) * 10
    // 如果等于 0 时, Slider选中第二格;(等于2时为标准大小)
    Decimal decimal =
        (Decimal.parse(_textScaleFactor.toString()) - Decimal.fromInt(1)) *
            Decimal.fromInt(10);
    sliderValue = decimal.toDouble();
    if (sliderValue == 0) {
      sliderValue = 2.0;
    } else {
      sliderValue = sliderValue + 2;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i18n(context).fontSize),
        centerTitle: true,
      ),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return Column(
      children: <Widget>[
        Expanded(
          child: Padding(
            padding: EdgeInsets.symmetric(
              vertical: Dimens.gapDp30,
              horizontal: Dimens.gapDp30,
            ),
            child: Text(
              "调整下方放大系数，App将会调整为下方您首先的阅读字体大小。",
              textAlign: TextAlign.center,
            ),
          ),
        ),
        Material(
          child: Container(
            height: 120.0,
            padding: EdgeInsets.symmetric(horizontal: Dimens.gapDp18),
            child: Row(
              children: <Widget>[
                Text(
                  "A",
                  style: TextStyle(fontSize: 12.0),
                  textScaler: TextScaler.linear(1.0),
                ),
                Expanded(
                  child: SliderTheme(
                    data: SliderTheme.of(context).copyWith(
                      // trackHeight: 2.0,
                      tickMarkShape: SliderTickMarkShape.noTickMark,
                      minThumbSeparation: 2.0,
                      // 实际进度颜色
                      // activeTrackColor: Colors.blue,
                      // 滑块中心颜色
                      // thumbColor: Colors.blue,
                      // 默认进度条颜色
                      // inactiveTickMarkColor: Colors.red,
                      // 提示气泡颜色
                      // valueIndicatorColor: Colors.blue,
                      // 提示气泡文字颜色
                      // valueIndicatorTextStyle: TextStyle(color: Colors.white),
                      // 对进度进行分割后, 断续线中间的颜色
                      // disabledInactiveTickMarkColor: Colors.red,
                      // 滑块边缘颜色
                      // overlayColor: Colors.pink,
                    ),
                    child: Slider(
                      label: "放大系数(${_scaleFactor(sliderValue)})",
                      value: sliderValue,
                      divisions: 7,
                      min: 0.0,
                      max: 7.0,
                      onChanged: (val) {
                        setState(() {
                          sliderValue = val;
                        });
                      },
                      onChangeEnd: (val) {
                        double scaleFactor = _scaleFactor(val);
                        provider.switchTextScaleFactor(scaleFactor);
                      },
                    ),
                  ),
                ),
                Text(
                  "A",
                  style: TextStyle(fontSize: 24.0),
                  textScaler: TextScaler.linear(1.0),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  // 根据slider选择值计算字体放大系数
  double _scaleFactor(double slider) {
    // 默认等于2时,为标准字体大小
    // 小于2时,缩小字体大小
    // 大于2时,放大字体大小
    // 放大系数为:(slider选择格数/10)
    // 放大后字体大小系数:(1.0 + 放大系数)
    double scaleFactor = 1.0;
    if (slider == 2) {
      scaleFactor = 1.0;
    } else if (slider < 2) {
      scaleFactor = 1.0 - ((2.0 - slider) / 10.0);
    } else {
      scaleFactor = 1.0 + (slider - 2) / 10.0;
    }
    // print("字体系数:$scaleFactor");
    return scaleFactor;
  }
}
